#include "xsip.h"
#include "msg.h"
#include "xcommandtransaction.h"
#include "xsmallrequest.h"
#include "xbigrequest.h"
#include <stdio.h>
#include "xdialogtransactionstate.h"
#include "xdialogtransaction.h"
#include "xcall.h"

XSIPPayloadOffsetHeader::XSIPPayloadOffsetHeader(XSIPHeader::header* header)
{
	sscanf(header->data, "%d %d %d", &m_contentIndex, &m_dataSize, &m_contentTotalSize);
}

int XSIPPayloadOffsetHeader::getContentIndex()
{
	return m_contentIndex;
}

int XSIPPayloadOffsetHeader::getDataSize()
{
	return m_dataSize;
}

int XSIPPayloadOffsetHeader::getContentTotalSize()
{
	return m_contentTotalSize;
}

char* XSIPPayloadOffsetHeader::getPayloadOffset(int i, int size, int total)
{
	static char buffer[32] = {0};
	sprintf(buffer, "%d %d %d", i, size, total);
	return buffer;
}

XSIPPayloadOffsetAckHeader::XSIPPayloadOffsetAckHeader(XSIPHeader::header* header)
{
	sscanf(header->data, "%d", &m_ack);
}

int XSIPPayloadOffsetAckHeader::getAck()
{
	return m_ack;
}

char* XSIPPayloadOffsetAckHeader::getPayloadOffsetAck(int ack)
{
	static char buffer[32] = {0};
	sprintf(buffer, "%d", ack);
	return buffer;
}

void Process::processRequest(XSIPTransport* transport, XSIPRequest* request)
{
	XSIPTransaction *transaction = new XSIPCommandTransaction();
	XSIPSmallServerTransactionState *state = new XSIPSmallServerTransactionState();
	transaction->setTransactionState(state);
	transaction->setTransport(transport);
	transaction->setTransactionId(request->getHeader()->getHeader(XSIP_MSG_HEADER_TRANSACTION_ID)->data);
	XSIPEngine::getCurrentThreadEngine()->addTransaction(transaction);
	state->setTransaction(transaction);
	transaction->setRequest(request);
	state->setState(TSX_STATE_RECV_REQUEST);
	state->touch();
}

void Process::processResponse(XSIPTransaction* transaction, XSIPResponse* response)
{
	XSIPTransactionState *state = transaction->getTranactionState();
	transaction->setResponse(response);
	state->setState(TSX_STATE_RECV_RESPONSE);
	state->touch();
}

void Process::processResponse(XSIPTransaction* transaction, XSIPPayloadOffsetHeader* payloadOffset, XSIPResponse* response)
{
	XSIPTransactionState *state = transaction->getTranactionState();
	transaction->setResponse(response);
	state->setState(TSX_STATE_RECV_N_RESPONSE);
	state->touch();
}

void Process::processRequest(XSIPTransaction* transaction, XSIPPayloadOffsetAckHeader* ack)
{
	XSIPTransactionState *state = transaction->getTranactionState();
	state->setState(TSX_STATE_RECV_1_REQUEST);
	state->touch();
}

void Process::processRequest(XSIPTransport* transport, XSIPPayloadOffsetHeader* payloadOffset, XSIPRequest* request)
{
	XSIPTransaction *transaction = new XSIPCommandTransaction();
	XSIPBigServerTransactionState *state = new XSIPBigServerTransactionState();
	transaction->setTransactionState(state);
	transaction->setTransport(transport);
	transaction->setTransactionId(request->getHeader()->getHeader(XSIP_MSG_HEADER_TRANSACTION_ID)->data);
	XSIPEngine::getCurrentThreadEngine()->addTransaction(transaction);
	state->setTransaction(transaction);
	transaction->setRequest(request);
	state->setState(TSX_STATE_RECV_N_REQUEST);
	state->touch();
}
	
void Process::processRequest(XSIPTransaction* transaction, XSIPPayloadOffsetHeader* payloadOffset, XSIPRequest* request)
{
	XSIPTransactionState *state = transaction->getTranactionState();
	state->setState(TSX_STATE_RECV_N_REQUEST);
	state->touch();
}
	
void Process::processResponse(XSIPTransaction* transaction, XSIPPayloadOffsetAckHeader* ack)
{
	XSIPTransactionState *state = transaction->getTranactionState();
	state->setState(TSX_STATE_RECV_1_RESPONSE);
	state->touch();
}

void Process::processInviteRequest(XSIPTransport* transport, XSIPRequest* request)
{
	XSIPDialogTransaction *transaction = new XSIPDialogTransaction(XSIPTransaction::TRANSACTION_TYPE_DIALOG_OPEN);
	transaction->setTransport(transport);
	XSIPDialogServerTransactionState *state = new XSIPDialogServerTransactionState(transaction);
	state->setTransaction(transaction);
	transaction->setTransactionState(state);
	transaction->setRequest(request);
	transaction->setTransactionId(request->getHeader()->getHeader(XSIP_MSG_HEADER_TRANSACTION_ID)->data);
	XSIPEngine::getCurrentThreadEngine()->addTransaction(transaction);

	state->setState(TSX_STATE_RECV_INVITE);
	state->touch();
}

void Process::processReInviteRequest(XSIPTransport* transport, XSIPDialog* dialog, XSIPRequest* request)
{
	XSIPDialogTransaction *transaction = new XSIPDialogTransaction(XSIPTransaction::TRANSACTION_TYPE_DIALOG_OPEN);
	XSIPDialogServerTransactionState *state = new XSIPDialogServerTransactionState(transaction);
	state->setTransaction(transaction);
	transaction->setTransactionState(state);
	transaction->setRequest(request);
	transaction->setTransactionId(request->getHeader()->getHeader(XSIP_MSG_HEADER_TRANSACTION_ID)->data);
	XSIPEngine::getCurrentThreadEngine()->addTransaction(transaction);
	transaction->setTransport(transport);

	state->setState(TSX_STATE_RECV_REINVITE);
	state->touch();
}

void Process::processInviteResponse(XSIPDialog* dialog, XSIPTransaction* transaction, XSIPResponse* response)
{
	transaction->setResponse(response);
	XSIPTransactionState* state = transaction->getTranactionState();
	state->setState(TSX_STATE_RECV_RESPONSE);
	state->touch();
}

void Process::processAck(XSIPDialog* dialog, XSIPTransaction* transaction, XSIPRequest* ack)
{
	XSIPTransactionState* state = transaction->getTranactionState();
	state->setState(TSX_STATE_RECV_ACK);
	state->touch();
}


DeleteCall::DeleteCall(XSIPCall* call)
	: DelayQueueEntry(DelayInterval(0, 0))
{
	m_call = call;
}

DeleteCall::~DeleteCall()
{

}

void DeleteCall::handleTimeout()
{
	delete m_call;
	delete this;
}